import * as Cesium from 'cesium'

export default class InundationAnalyse {
  handler
  listener
  constructor(viewer, div) {
    this.viewer = viewer
    this.tempEntities = new Cesium.EntityCollection()    // 要素集合
    this.div = div   // 按钮元素
    this.waterMinElevation = 200    // 最小水位高程
    this.waterMaxElevation = 800    // 最大水位高程
  }
  // 绘制点
  drawPoint(position) {
    let point = this.viewer.entities.add({
      name: '点几何对象',
      position: position,
      point: {
        color: Cesium.Color.SKYBLUE,
        pixelSize: 10,
        outlineColor: Cesium.Color.YELLOW,
        outlineWidth: 3,
        disableDepthTestDistance: Number.POSITIVE_INFINITY,
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
      }
    })
    this.tempEntities.add(point)
    return point
  }
  // 绘制线
  drawPolyline(positions) {
    if (positions.length < 1) return
    let line = this.viewer.entities.add({
      name: "线几何对象",
      polyline: {
        positions: positions,
        width: 5.0,
        material: new Cesium.PolylineGlowMaterialProperty({
          color: Cesium.Color.GOLD,
        }),
        depthFailMaterial: new Cesium.PolylineGlowMaterialProperty({
          color: Cesium.Color.GOLD,
        })
      }
    })
    this.tempEntities.add(line)
    return line
  }
  // 绘制面
  drawPolygon(positions) {
    let polygon = this.viewer.entities.add({
      name: '面几何对象',
      polygon: {
        hierarchy: new Cesium.PolygonHierarchy(positions),
        extrudedHeight: new Cesium.CallbackProperty(() => {
          if (this.waterMinElevation < this.waterMaxElevation) {
            this.waterMinElevation += 1
          }
          return this.waterMinElevation
        }, false),
        material: new Cesium.Color.fromCssColorString("#3D81A5").withAlpha(0.7)
      }
    })
    this.tempEntities.add(polygon)
    return polygon
  }
  // 添加淹没分析
  addInundationAnalysis() {
    const that = this
    // 移除默认双击事件trackedEntity
    that.viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
    // 开启深度检测
    that.viewer.scene.globe.depthTestAgainstTerrain = true
    that.listener = () => {
      let tempPoints = []    // 放置添加线时点的坐标数组
      let point = that.viewer.entities.add({
        point: {
          color: Cesium.Color.SKYBLUE,
          pixelSize: 10,
          outlineColor: Cesium.Color.YELLOW,
          outlineWidth: 3,
          disableDepthTestDistance: Number.POSITIVE_INFINITY
        }
      })
      that.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas)
      //鼠标移动事件
      that.handler.setInputAction(function (movement) {
        let position = that.viewer.camera.pickEllipsoid(movement.endPosition, that.viewer.scene.globe.ellipsoid)
        point.position = position
      }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
      //左键点击操作
      that.handler.setInputAction(function (click) {
        //调用获取位置信息的接口，加载地形时使用，得到的是Cartesian3三维坐标
        let ray = that.viewer.camera.getPickRay(click.position)
        let position = that.viewer.scene.globe.pick(ray, that.viewer.scene)
        tempPoints.push(position)
        let tempLength = tempPoints.length
        //调用绘制点的接口
        that.drawPoint(position)
        if (tempLength > 1) {
          that.drawPolyline([tempPoints.at(-2), tempPoints.at(-1)])
        }
      }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
      //双击点击绘制完成操作
      that.handler.setInputAction(function (click) {
        let cartesian = that.viewer.camera.pickEllipsoid(click.position, that.viewer.scene.globe.ellipsoid)
        if (cartesian) {
          let tempLength = tempPoints.length
          if (tempLength < 3) {
            alert('请选择3个以上的点再执行闭合操作命令')
          } else {
            that.viewer.entities.remove(point)   // 当绘制第二个点时，需要把移动事件那个点在图上删除。
            //闭合最后一条线
            that.drawPolyline([tempPoints.at(-1), tempPoints[0]])
            let entity = that.drawPolygon(tempPoints)
            that.handler.destroy()//关闭事件句柄
            that.viewer.flyTo(entity)
          }
        }
      }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
    }
    that.div.addEventListener('click', that.listener)
  }
  // 移除淹没分析
  removeInundationAnalysis() {
    this.viewer.entities.removeAll()
    this.tempEntities.removeAll()
    this.div.removeEventListener('click', this.listener)
    this.listener = null
  }
}